package com.ygo.feihua.Management;


import android.util.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.baidu.mapapi.*;
import com.xiaomi.channel.commonutils.logger.*;
import com.xiaomi.mipush.sdk.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.logo.*;
import java.util.*;
import android.content.*;
import com.xiaomi.mistatistic.sdk.*;
import com.xiaomi.mistatistic.sdk.controller.*;
import com.xiaomi.mistatistic.sdk.data.*;
import org.json.*;

public class SdkInitManagement
{
	private final static SdkInitManagement mSdkInitManagement=new SdkInitManagement();;
	
	private boolean isInitBmobSdk=false;
	private boolean isInitBaiduMapSdk=false;
	//private boolean isInitPgySdk;
	private boolean isInitMiPushSdk=false;
	private boolean isInitMiStats=false;
	
	
	private SdkInitManagement(){
		
	}
	
	public static SdkInitManagement getDx(){
		return mSdkInitManagement;
	}
	
	public void initAllSdk(){
		initBmobSdk();
	//	initPgySdk();
		initBaiduMapSdk();
		initMiPushSdk();
		initMiStats();
	}
	
	public void initBmobSdk(){
		if(!isInitBmobSdk){
			isInitBmobSdk=true;
			Bmob.initialize(OurygoApplication.getContext(),"bmob app key");
			userUpdate();
			BmobInstallationManager.getInstance().initialize(new InstallationListener<BmobInstallation>() {
					@Override
					public void done(BmobInstallation bmobInstallation, BmobException e) {
						
					}
				});
			// 启动推送服务
			//BmobPush.startWork(OurygoApplication.getContext());
			
		}
	}
	
	public void initBaiduMapSdk(){
		if(!isInitBaiduMapSdk){
			isInitBaiduMapSdk=true;
			SDKInitializer.initialize(OurygoApplication.getContext());
		}
	}
	
	/*public void initPgySdk(){
		if(!isInitPgySdk){
			isInitPgySdk=true;
			PgyCrashManager.register(OurygoApplication.getContext());
			Pgy.init(OurygoApplication.getContext(),"b2e578a0b2060526852947a7d9c5ad20");
		}
	}*/
	
	public void initMiStats(){
		if(!isInitMiStats){
			isInitMiStats=true;
			MiStatInterface.initialize(OurygoApplication.getContext(), OurygoApplication.MI_PUSH_APP_ID, OurygoApplication.MI_PUSH_APP_KEY,"ourygo");
			MiStatInterface.setUploadPolicy(MiStatInterface.UPLOAD_POLICY_WHILE_INITIALIZE, 0);
			MiStatInterface.enableLog();
			MiStatInterface.enableExceptionCatcher(true);
			URLStatsRecorder.enableAutoRecord();
			URLStatsRecorder.setEventFilter(new HttpEventFilter() {

					@Override
					public HttpEvent onEvent(HttpEvent event) {
						/*try
						{
							Log.e("MI_STAT", event.getUrl() + " result =" + event.toJSON());
						}
						catch (JSONException e)
						{
							Log.e("Mi",e+"");
							
						}*/
						
						// returns null if you want to drop this event.
						// you can modify it here too.
						return event;
					}
				});
		}
	}
	
	public void initMiPushSdk(){
		if(!isInitMiPushSdk){
			isInitMiPushSdk=true;
		MiPushClient.registerPush(OurygoApplication.getContext(), OurygoApplication.MI_PUSH_APP_ID, OurygoApplication.MI_PUSH_APP_KEY);

		if(isInitBmobSdk){
		if(BmobUser.getCurrentUser(MyUser.class)!=null){
		MiPushClient.setAlias(OurygoApplication.getContext(),BmobUser.getCurrentUser(MyUser.class).getUsername(),null);
		}
		}
        //打开Log
        LoggerInterface newLogger = new LoggerInterface() {



            @Override
            public void setTag(String tag) {
                // ignore   
            }

            @Override
            public void log(String content, Throwable t) {
          //      Log.d(TAG, content, t);
            }

            @Override
            public void log(String content) {
              //  Log.d(TAG, content);
            }
        };
        Logger.setLogger(OurygoApplication.getContext(), newLogger);
	}
	}
	

	//更新用户信息
	private void userUpdate(){
		final UserManagement um=UserManagement.getDx();
		if(um.getUser()!=null){
			//根据本地缓存的用户名查询云端用户
			BmobQuery<MyUser> my=new BmobQuery<MyUser>();
			my.addWhereEqualTo("username",um.getUser().getUsername());
			my.findObjects(new FindListener<MyUser>(){

					@Override
					public void done(List<MyUser> p1, BmobException p2)
					{
						if(p2==null){
							//如果该用户不存在/用户不能登录/用户未验证邮箱则清除本地用户缓存
							if(p1.size()==0||p1.get(0).getUserzt()==false||p1.get(0).getEmailVerified()==false){
								um.logOutUser();
								Gongju.show("用户信息已过期,请重新登录");
							}else{
								//否则更新用户信息
								p1.get(0).update(new UpdateListener(){

										@Override
										public void done(BmobException p1)
										{
											// TODO: Implement this method
										}
									});
							}
						}
						// TODO: Implement this method
					}

				});
		}
	}
	
}
